package com.xuecheng.media.api;

import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @description: 上传视频
 * @author: ajie
 * @date: 2024/1/19 21:07:55
 * @version: 1.0
 */
@Api(value = "大文件上传接口",tags = "大文件上传接口")
@RestController
public class BigFilesController {

    @Autowired
    MediaFileService mediaFileService;


    /**
     * @description: 文件上传前检查文件
     * @params: [fileMd5]
     * @return:
     * @author: ajie
     * @date: 2024/1/19 21:16
     */
    @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkfile(@RequestParam("fileMd5") String fileMd5)throws Exception{
        RestResponse<Boolean> restResponse = mediaFileService.checkfile(fileMd5);
        return restResponse;
    }

    /**
     * @description: 分块文件上传前的检测
     * @params: [fileMd5, chunk]
     * @return:
     * @author: ajie
     * @date: 2024/1/19 21:20
     */
    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") int chunk)throws Exception{
        RestResponse<Boolean> restResponse = mediaFileService.checkchunk(fileMd5,chunk);
        return restResponse;
    }

    /**
     * @description: 上传分块文件
     * @params: [file, fileMd5, chunk]
     * @return:
     * @author: ajie
     * @date: 2024/1/19 21:25
     */
    @ApiOperation(value = "上传分块文件")
    @PostMapping("/upload/uploadchunk")
    public RestResponse uploadchunk(@RequestParam("file")MultipartFile file,
                                    @RequestParam("fileMd5")String fileMd5,
                                    @RequestParam("chunk")int chunk)throws Exception{

        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        //拷贝到临时文件
        file.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();
        RestResponse restResponse = mediaFileService.uploadChunk(fileMd5, chunk, localFilePath);

        return restResponse;
    }

    /**
     * @description: 合并文件
     * @params: [fileMd5, fileName, chunkTotal]
     * @return:
     * @author: ajie
     * @date: 2024/1/19 21:25
     */
    @ApiOperation(value = "合并文件")
    @PostMapping("/upload/mergechunks")
    public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal) throws Exception {

        Long companyId = 1232141425L;
        //文件信息对象
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        uploadFileParamsDto.setTags("视频文件");
        uploadFileParamsDto.setFileType("001002");
        RestResponse mergechunks = mediaFileService.mergechunks(companyId, fileMd5, chunkTotal, uploadFileParamsDto);
        return mergechunks;

    }

}
